unit MeasurementUnitTable;

{$mode objfpc}{$H+}

//========================================================================================
//
// Unit : MeasurementUnitTable.pas
//
// Description :
//
// Called By :
//
// Calls : ASDLDataModule : dmASLData.SQLite3ConnectionASLDB
//                          dmASLData.SQLTransactionASLDB
//         Hutils : ConfirmationMessageDlg
//         MeasurementUnitData : frmMeasurementUnitData.ShowModal
//
// Ver. : 1.0.0
//
// Date : 19 December 2016
//
// TO DO
//        1. Set command buttons as required for all functions
//        2. Do not allow deletion of Inches and CM
//        3. Do not reuse ID numbers for values that have been deleted. This will
//           require an additional column "Active" in the table.
//
//========================================================================================

interface

uses
  Classes, SysUtils, sqldb, db, FileUtil, Forms, Controls, Graphics, Dialogs,
  DBGrids, Buttons, Menus, DbCtrls, StdCtrls,
  // Application Units
  ASLDataModule, HUtils, MeasurementUnitData;

type

  { TfrmMeasurementUnitTable }

  TfrmMeasurementUnitTable = class(TForm)
    bbtCancel: TBitBtn;
    bbtHelp: TBitBtn;
    bbtSave: TBitBtn;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    SQLQuery1: TSQLQuery;
    procedure bbtCancelClick(Sender: TObject);
    procedure bbtHelpClick(Sender: TObject);
    procedure bbtSaveClick(Sender: TObject);
    procedure DBNavigator1BeforeAction(Sender: TObject; Button: TDBNavButtonType);
    procedure DBNavigator1Click(Sender: TObject; Button: TDBNavButtonType);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormRefresh;
    procedure PopulateProperties;
  private
    { private declarations }
//    fProcessType : tProcessType;
    fUnitID : integer;
    fUnitName : string;
  public
    { public declarations }
//    function GetProcessType: tProcessType;
//    procedure SetProcessType ( PType :tProcessType);
    function GetUnitID: integer;
    procedure SetUnitID (intID:integer);
    function GetUnitName: string;
    procedure SetUnitName (strName:string);
  published
    { published declarations }
//    property ProcessType: tProcessType read GetProcessType write SetProcessType;
    property UnitID: integer read GetUnitID write SetUnitID;
    property UnitName: string read GetUnitName write SetUnitName;
  end;

var
  frmMeasurementUnitTable: TfrmMeasurementUnitTable;

implementation

{$R *.lfm}

var
  vblnTableChanged : boolean;
  vintRecCount : integer;
  vintNewMeasurementUnitID : Integer;
//  vblnDone : boolean;

//========================================================================================
//          PRIVATE CONSTANTS
//========================================================================================

//========================================================================================
//          PUBLIC CONSTANTS
//========================================================================================

//========================================================================================
//          PRIVATE VARIABLES
//========================================================================================

//========================================================================================
//          PUBLIC VARIABLES
//========================================================================================

//========================================================================================
//          PRIVATE ROUTINES
//========================================================================================
function TfrmMeasurementUnitTable.GetUnitID: integer;
begin
  Result := fUnitID;
end;// function TfrmMeasurementUnitTable.GetUnitID

//----------------------------------------------------------------------------------------
procedure TfrmMeasurementUnitTable.SetUnitID (intID:integer);
begin
  fUnitID := intID;
end;// procedure TfrmMeasurementUnitTable.SetUnitID

//========================================================================================
function TfrmMeasurementUnitTable.GetUnitName: string;
begin
  Result := fUnitName;
end;// function TfrmMeasurementUnitTable.SetUnitID

//----------------------------------------------------------------------------------------
procedure TfrmMeasurementUnitTable.SetUnitName (strName:string);
begin
  fUnitName := strName;
end;// procedure TfrmMeasurementUnitData.SetUnitName
//========================================================================================
procedure TfrmMeasurementUnitTable.PopulateProperties;
begin
//  edtUnitID.text:=IntToStr(SQLQuery1.FieldByName('MeasurementUnitID').AsInteger);
//  edtUnitName.text:=SQLQuery1.FieldByName('MeasurementUnitName').AsString;
end;// procedure TfrmMeasurementUnitTable.PopulateProperties

//========================================================================================
//          PUBLIC ROUTINES
//========================================================================================

//========================================================================================
//          PROPERTY ROUTINES
//========================================================================================

//========================================================================================
//          MENU ROUTINES
//========================================================================================

//========================================================================================
//          COMMAND BUTTON ROUTINES
//========================================================================================
procedure TfrmMeasurementUnitTable.bbtCancelClick(Sender: TObject);
begin
     // Close without committing
  showmessage ('Close with no commit');
end;// procedure TfrmMeasurementUnitTable.bbtCancelClick

//----------------------------------------------------------------------------------------
procedure TfrmMeasurementUnitTable.bbtHelpClick(Sender: TObject);
begin
     // No enabled
end;// procedure TfrmMeasurementUnitTable.bbtHelpClick

//----------------------------------------------------------------------------------------
procedure TfrmMeasurementUnitTable.bbtSaveClick(Sender: TObject);
begin
     // Commit and save
  showmessage('Commit and close');
end;// procedure TfrmMeasurementUnitTable.bbtSaveClick

//========================================================================================
//          CONTROL ROUTINES
//========================================================================================

//          DBNavigator
procedure TfrmMeasurementUnitTable.DBNavigator1BeforeAction(Sender: TObject;
  Button: TDBNavButtonType);
begin

  case Button of
    nbFirst: begin
                  SQLQuery1.First;
                  PopulateProperties;
                  Abort;
             end;// nbFirst
    nbPrior: begin
                  SQLQuery1.Prior;
                  PopulateProperties;
                  Abort;
              end;// nbPrior
    nbNext: begin
                  SQLQuery1.Next;
                  PopulateProperties;
                  Abort;
              end;// nbNext
    nbLast: begin
                  SQLQuery1.Last;
                  PopulateProperties;
                  Abort;
              end;// nbLast
    nbInsert: begin
                frmMeasurementUnitData.ProcessType:=ptInsert;
                frmMeasurementUnitData.UnitID := SQLQuery1.RecordCount + 1;
                frmMeasurementUnitData.UnitName := '';
                if frmMeasurementUnitData.ShowModal = mrCancel then
                Abort;
                SQLQuery1.Insert;
                SQLQuery1.SQL.text:='INSERT INTO MeasurementUnitTable VALUES (:MeasurementUnitID, :MeasurementUnitName);';
                SQLQuery1.Params.ParamByName('MeasurementUnitID').AsInteger := UnitID;
                SQLQuery1.Params.ParamByName('MeasurementUnitName').AsString := UnitName;
                SQLQuery1.ExecSQL;
                SQLQuery1.Post;
                FormRefresh;
                Abort;
              end;// nbInsert
    nbDelete: begin
                if HUtils.ConfirmationMessageDlg( 'Confirm Deletion',
                                                  'Confirm you wish to Delete' +
                                                  #13 +
                                                  'the Current Record.') = mrNo then
                Abort;
                SQLQuery1.Close;
                SQLQuery1.SQL.text:='DELETE FROM MeasurementUnitTable WHERE MeasurementUnitID = :MeasurementUnitID;';
                SQLQuery1.Params.ParamByName('MeasurementUnitID').AsInteger := UnitID;
                SQLQuery1.ExecSQL;
                FormRefresh;
                Abort;
              end;
    nbEdit: begin
                  Abort;
              end;// nbEdit
    nbPost: begin
                  Abort;
            end;// nbPost
    nbCancel: begin
                  Abort;
              end;// nbCancel
    nbRefresh: begin
                  Abort;
               end;// nbRefresh
  end;// case Button

end;// procedure TfrmMeasurementUnitTable.DBNavigator1BeforeAction

//----------------------------------------------------------------------------------------
procedure TfrmMeasurementUnitTable.DBNavigator1Click(Sender: TObject;
  Button: TDBNavButtonType);
begin

{  case Button of
   { nbFirst : begin
                SQLQuery1.First;
              end;// nbFirst
    nbPrior : begin
                  SQLQuery1.Prior;
              end;// nbPrior
    nbNext : begin
                  SQLQuery1.Next;
              end;// nbNext
    nbLast : begin
                  SQLQuery1.Last;
              end;// nbLast }
    nbInsert : begin
                edtUnitID.Text := '99';
              end;// nbInsert
{    nbDelete : begin
                Abort;
              end;// nbDelete
    nbEdit : begin
                Abort;
              end;// nbEdit
    nbPost : begin
                  Abort;
             end;// nbPost
    nbCancel : begin
                  Abort;
             end;// nbCancel
    nbRefresh : begin
                  Abort;
             end;// nbRefresh }

  end;// case Button }

end;// procedure TfrmMeasurementUnitTable.DBNavigator1Click

//========================================================================================
//          FILE ROUTINES
//========================================================================================

//========================================================================================
//          FORM ROUTINES
//========================================================================================
procedure TfrmMeasurementUnitTable.FormCreate(Sender: TObject);
begin

  // First we Setup the DBGrid
  DBGrid1.Columns[0].FieldName := 'MeasurementUnitID';
  DBGrid1.Columns[0].Alignment := taCenter;
  DBGrid1.Columns[0].Width := 40;
  DBGrid1.Columns[0].Title.Caption := 'ID';
  DBGrid1.Columns[0].Title.Alignment := taCenter;

  DBGrid1.Columns[1].FieldName := 'MeasurementUnitName';
  DBGrid1.Columns[1].Alignment := taCenter;
  DBGrid1.Columns[1].Width := 70;
  DBGrid1.Columns[1].Title.Caption := 'Unit';
  DBGrid1.Columns[1].Title.Alignment := taCenter;

end;// procedure TfrmMeasurementUnitTable.FormCreate

//========================================================================================
procedure TfrmMeasurementUnitTable.FormShow(Sender: TObject);
begin

  // Connect the data controls
  SQLQuery1.DataBase := dmASLData.SQLite3ConnectionASLDB;
  SQLQuery1.Transaction := dmASLData.SQLTransactionASLDB;

  DataSource1.DataSet := SQLQuery1;

  DBGrid1.DataSource := DataSource1;

  DBNavigator1.DataSource := DataSource1;

  // Open the Table
  FormRefresh;

  // Initialize the Command Buttons
  bbtHelp.Enabled := False;
  bbtCancel.Enabled := True;
  bbtSave.Enabled := False;

end;// procedure TfrmMeasurementUnitTable.FormShow

//========================================================================================
procedure TfrmMeasurementUnitTable.FormRefresh;
begin
  SQLQuery1.Close;
  SQLQuery1.SQL.Text:='Select * From MeasurementUnitTable';
  SQLQuery1.Open;
  PopulateProperties;
end;// TfrmMeasurementUnitTable.FormRefresh

//========================================================================================
end.// unit MeasurementUnitTable

